<?php
namespace app\admin\controller;
use app\model\NewsTag as NewsTagModel;
use think\Db;


class NewsTag extends Base
{
    public function __construct(){
        $model = new NewsTagModel();
        $this->model = $model;
        parent::__construct();
        
        $page_info = $this->page_info;
        $page_info['title'] = '分类';

        $this->assign([
            'model'=>$this->model,
            'page_info'=>$page_info
        ]);
    }

    public function assignFields(){
        $model = $this->model;

        $categorys = Db::name('category')->where('pid',0)->select();
        $categorys = arrayParamExtract($categorys,'id','name');
        $fields = [
            'name'=>['name'=>'名称','type'=>'text','valids'=>['required']],
            'content'=>['name'=>'原文','type'=>'textarea'],
        ];
        $valids = createPageValid($fields);
        $this->assign([
            'fields'=>$fields,
            'valids'=>$valids
        ]);
    }

    public function assignSearchs(){
        $model = $this->model;

        $search_params = [
            [
                ['key'=>'name','name'=>'名称','type'=>'text'],
            ]
        ];
        $this->assign([
            'search_params'=>$search_params
        ]);
    }

    public function index($news_id){
        $model = $this->model;
        $map_params = [
            ['key'=>'name','type'=>'like'],
        ];
        $map = getSearchParam($map_params);
        $map[] = ['news_id','=',$news_id];

        $this->assignSearchs();

        $page_param = [
            'page'=>input('page',1),
            'list_rows'=>input('page_size',20)
        ];
        $list = $model->where($map)->order('id','desc')->paginate($page_param);
        $page = $list->render();

        $list = $list->toarray();
        foreach ($list['data'] as $k => $vo) {
           
        }
        return view('index', [
            'list'=>$list,
            'page'=>$page
        ]);
    }


    public function add(){
        if (request()->isPost()) {
            $fields = [
                'must'=>['news_id','name'],
                'nomust'=>['content']
            ];
            $save_data = paramFilter(request()->param(),$fields);
            if (!empty($save_data['error_msg'])) {
                $this->error($save_data['error_msg']);
            }
        }
        
        $model = $this->model;
        if (request()->isPost()) {
            $t_fields = $model->getTableFields();
            if (!isset($save_data['ctime']) && in_array('ctime', $t_fields)) {
                $save_data['ctime'] = time();
            }
            if (!isset($save_data['utime']) && in_array('utime', $t_fields)) {
                $save_data['utime'] = time();
            }
            if ( $model->save($save_data) ) {
                $this->success("添加成功",url('index',['news_id'=>$save_data['news_id']]));
            }else{
                $this->error("添加失败");
            }
        }

        $this->assignFields();
        return view(emptyDef($this->page_info['view'],'common/save'), [
            
        ]);
    }

    public function edit(){
        $model = $this->model;
        $id = input('id');
        if (empty($id)) { $this->error('id不能为空'); }
        $data = $model->where('id',$id)->find();
        
        if (request()->isPost()) {
            $fields = [
                'nomust'=>['name','content']
            ];
            $save_data = paramFilter(request()->param(),$fields);
            if (!empty($save_data['error_msg'])) {
                $this->error($save_data['error_msg']);
            }
        }
        
        if (request()->isPost()) {
            $t_fields = $model->getTableFields();
            if (!isset($save_data['utime']) && in_array('utime', $t_fields)) {
                $save_data['utime'] = time();
            }

            if ( $model->where('id',$id)->update($save_data) !== false ) {
                $this->success("编辑成功",url('index',['news_id'=>$data['news_id']]));
            }else{
                $this->error("编辑失败");
            }
        }

        $this->assignFields();
        return view(emptyDef($this->page_info['view'],'common/save'), [
            'data'=>$data
        ]);
    }




   
}
